package com.ruoyi.group.service.impl;

import java.util.List;

import com.ruoyi.data.DataUtils;
import com.ruoyi.door.domain.HouseDoorT;
import com.ruoyi.door.mapper.HouseDoorTMapper;
import com.ruoyi.unit.domain.HouseUnitT;
import com.ruoyi.unit.mapper.HouseUnitTMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.group.mapper.HouseGroupTMapper;
import com.ruoyi.group.domain.HouseGroupT;
import com.ruoyi.group.service.IHouseGroupTService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 楼宇管理Service业务层处理
 *
 * @author lijie
 * @date 2023-04-19
 */
@Service
public class HouseGroupTServiceImpl implements IHouseGroupTService
{
    @Autowired
    private HouseGroupTMapper houseGroupTMapper;
    @Autowired
    private HouseUnitTMapper houseUnitTMapper;
    @Autowired
    private HouseDoorTMapper houseDoorTMapper;

    /**
     * 查询楼宇管理
     *
     * @param groupId 楼宇管理主键
     * @return 楼宇管理
     */
    @Override
    public HouseGroupT selectHouseGroupTByGroupId(Long groupId)
    {
        return houseGroupTMapper.selectHouseGroupTByGroupId(groupId);
    }

    @Override
    public HouseGroupT selectGroup(HouseGroupT houseGroupT) {
        return houseGroupTMapper.selectGroup(houseGroupT);
    }

    /**
     * 查询楼宇管理列表
     *
     * @param houseGroupT 楼宇管理
     * @return 楼宇管理
     */
    @Override
    public List<HouseGroupT> selectHouseGroupTList(HouseGroupT houseGroupT)
    {
        return houseGroupTMapper.selectHouseGroupTList(houseGroupT);
    }

    @Override
    public List<HouseGroupT> selectAreaGroupTList(HouseGroupT houseGroupT){
    return houseGroupTMapper.selectAreaGroupTList(houseGroupT);
    }

    /**
     * 新增楼宇管理
     *
     * @param houseGroupT 楼宇管理
     * @return 结果
     */
    @Transactional
    @Override
    public int insertHouseGroupT(HouseGroupT houseGroupT)
    {
        return houseGroupTMapper.insertHouseGroupT(houseGroupT);
    }

    /**
     * 修改楼宇管理
     *
     * @param houseGroupT 楼宇管理
     * @return 结果
     */
    @Transactional
    @Override
    public int updateHouseGroupT(HouseGroupT houseGroupT)
    {
        houseGroupTMapper.updateHouseGroupUnitT(houseGroupT);
        houseGroupTMapper.updateHouseGroupDoorT(houseGroupT);
        return houseGroupTMapper.updateHouseGroupT(houseGroupT);
    }

    @Transactional
    @Override
    public int moveHouseGroupT(HouseGroupT houseGroupT)
    {
        HouseGroupT houseGroupT1 = houseGroupTMapper.selectHouseGroupTByGroupId(houseGroupT.getGroupId());
        houseGroupT1.setAreaName(houseGroupT.getAreaName());
        houseGroupT.setGroupName(houseGroupT1.getGroupName());
        HouseGroupT houseGroupT2 = houseGroupTMapper.selectGroup(houseGroupT1);
        if(houseGroupT2==null){
            houseGroupTMapper.insertHouseGroupT(houseGroupT1);
        }
        List<HouseUnitT> unitList = houseGroupTMapper.selectHouseGroupUnitT(houseGroupT);
        for(HouseUnitT houseUnitT : unitList){
            houseUnitT.setAreaName(houseGroupT.getAreaName());
            HouseUnitT houseUnitT1 = houseUnitTMapper.selectHouseUnitT(houseUnitT);
            if(houseUnitT1==null){
                houseUnitTMapper.insertHouseUnitT(houseUnitT);
            }
        }
        List<HouseDoorT> doorList = houseGroupTMapper.selectHouseGroupDoorT(houseGroupT);
        for(HouseDoorT houseDoorT : doorList){
            houseDoorT.setAreaName(houseGroupT.getAreaName());
            houseGroupT.setUnitName(houseDoorT.getUnitName());
            houseGroupT.setDoorName(houseDoorT.getDoorName());
            HouseDoorT houseDoorT1 = houseDoorTMapper.selectDoor(houseDoorT);
            if(houseDoorT1==null){
                houseGroupTMapper.updateHouseGroupDoorT(houseGroupT);
            }
        }
        return 1;
//        return houseGroupTMapper.updateHouseGroupT(houseGroupT);
//        return houseGroupTMapper.updateHouseGroupUnitT(houseGroupT);
    }

    @Transactional
    @Override
    public int updateHouseGroupPurposeT(List<HouseDoorT> doorList,HouseDoorT houseDoorT)
    {
        for(HouseDoorT doorVO:doorList){
            doorVO.setBudgetMoney(DataUtils.doubleScale2(DataUtils.multiplyMoney(doorVO.getSpace(), houseDoorT.getStandard())));
            doorVO.setPurposeId(houseDoorT.getPurposeId());
            doorVO.setPrice(houseDoorT.getStandard());
            houseGroupTMapper.updateHouseGroupPurposeT(doorVO);
        }
        return 1;
    }

    /**
     * 批量删除楼宇管理
     *
     * @param groupIds 需要删除的楼宇管理主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteHouseGroupTByGroupIds(Long[] groupIds)
    {
        return houseGroupTMapper.deleteHouseGroupTByGroupIds(groupIds);
    }

    /**
     * 删除楼宇管理信息
     *
     * @param groupId 楼宇管理主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteHouseGroupTByGroupId(Long groupId)
    {
        return houseGroupTMapper.deleteHouseGroupTByGroupId(groupId);
    }

    @Override
    public HouseGroupT getDoorNumber(HouseGroupT houseGroupT) {
        return houseGroupTMapper.getDoorNumber(houseGroupT);
    }


    @Override
    public HouseGroupT getGroupCost(HouseGroupT houseGroupT) {
        return houseGroupTMapper.getGroupCost(houseGroupT);
    }


    @Override
    public HouseGroupT getMust(HouseGroupT houseGroupT) {
        return houseGroupTMapper.getMust(houseGroupT);
    }


    @Override
    public HouseGroupT getAlready(HouseGroupT houseGroupT) {
        return houseGroupTMapper.getAlready(houseGroupT);
    }

    @Override
    public List<HouseGroupT> selectHouseGroupTByGroupName(HouseGroupT houseGroupT) {
        return houseGroupTMapper.selectHouseGroupTByGroupName(houseGroupT);
    }

}
